package com.alienware.carlauncherlite.Dock;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alienware.carlauncherlite.R;

import java.util.ArrayList;
import java.util.List;

public class DockDialogAdapter extends BaseAdapter {
    Context context;
    List<DockItemInfo> dockItemInfoList = new ArrayList<DockItemInfo>();

    public DockDialogAdapter(List<DockItemInfo> dockItemInfoList, Context context) {
        this.dockItemInfoList = dockItemInfoList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return dockItemInfoList.size();
    }

    @Override
    public Object getItem(int i) {
        return dockItemInfoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder = null;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.gridview_dialog_item, null);
            holder = new Holder();
            holder.appIcon = (ImageView) view.findViewById(R.id.dialogGridViewItemIcon);
            holder.appName = (TextView) view.findViewById(R.id.dialogGridViewItemName);
            holder.linearLayout = (LinearLayout) view.findViewById(R.id.dialogGridViewLinearLayout);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        if (dockItemInfoList.get(i).getChangeIcon() == 0) {
            holder.appIcon.setImageBitmap(dockItemInfoList.get(i).getAppIcon());
        } else {
            holder.appIcon.setImageResource(dockItemInfoList.get(i).getChangeIcon());
        }
        holder.appName.setText(dockItemInfoList.get(i).getAppName());
        return view;
    }

    private void getAppIconSize(Context context, Holder holder) {
        try {
            SharedPreferences sharedPreferences = context.getSharedPreferences("info", Context.MODE_PRIVATE);
            int size = Integer.parseInt(sharedPreferences.getString("icon_size", null));
            ViewGroup.LayoutParams params = holder.appIcon.getLayoutParams();
            params.height = size;
            params.width = size;
            holder.appIcon.setLayoutParams(params);
        } catch (Exception e) {
            SharedPreferences sharedPreferences = context.getSharedPreferences("info", Context.MODE_PRIVATE);
            sharedPreferences.edit().putString("icon_size", "45").apply();
            getAppIconSize(context, holder);
        }
    }

    private static class Holder {
        ImageView appIcon;
        TextView appName;
        LinearLayout linearLayout;
    }
}